/*
 * All rights Reserved, Designed By DataDriver
 * Copyright:    DataDriver.Inc
 * Company:      Zhuo Wo Infomation Technology (ShangHai) CO.LTD
 */
package com.sun.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.*;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

@Configuration
@ComponentScan("com.sun")
@EnableTransactionManagement   //事务管理器
public class TransConfig {

    @Bean
    public DataSource dataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser("sunyuanhang");
        dataSource.setPassword("sunyuanhang@666");
        dataSource.setJdbcUrl("jdbc:mysql://150.158.163.91:3306/boot");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


    //将事务过利器放入IOC容器中
    @Bean
    public PlatformTransactionManager platformTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


    /**
     * @EnableTransactionManagement原理：
     * 首先他导入组件选择器：@Import(TransactionManagementConfigurationSelector.class)
     * 默认导入两个组价： AutoProxyRegistrar；ProxyTransactionManagementConfiguration
     *
     * AutoProxyRegistar 向IOC容器中导入InfrastructureAdvisorAutoProxyCreator组件
     *
     */

}
